import {ref, toRefs} from 'vue';
import {dictDataTypeGET} from '@/services/system/dict/dict.service';
import {useDictStore} from '@/models/dict';

/**
 * 获取字典数据
 */
export default function useDict(...args: any[]) {
  const {getDict, setDict} = useDictStore();
  const res = ref({} as any);
  return (() => {
    args.forEach(async (dictType, index) => {
      res.value[dictType] = [];
      const dicts = getDict(dictType);
      if (dicts) {
        res.value[dictType] = dicts;
      } else {
        const result = await dictDataTypeGET(dictType);
        if (result?.code === 200) {
          res.value[dictType] = result?.data?.map((item: any) => {
            return {
              label: item.dictLabel,
              value: item.dictValue,
              elTagType: item.listClass,
              elTagClass: item.cssClass,
            };
          });
          setDict(dictType, res.value[dictType]);
        }
        console.log('字典', result);
      }
    });
    return toRefs(res.value);
  })();
}
